$(function(){
	loadItemListRecycleBin();
	selectItemRecycleBin(false);
	selectItemBrandsRecycleBin();
	selectItemCatTreeRecycleBin();
});

/**
 * 加载商品列表
 */
function loadItemListRecycleBin(){
	$('#itemrecyclebin_dategrid').datagrid({
		loadMsg : "数据加载中",
		title:"商品列表",
		border:true,
		fit : true,
		fitColumns : true,
		pagination : true,
		rownumbers : true,
		singleSelect : false,
		toolbar : "#itemrecyclebin_dategrid_toolbar",
		columns : [ [
		    {field:	 'checked',checkbox:true},
		    {field : 'id',hidden : true},
		    {field : 'categoryId',hidden : true},
		    {field : 'brandId',hidden : true},
		    {field : 'productCode',hidden : true},
		    {field : 'unit',hidden : true},
		    {field : 'summary',hidden : true},
		    {field : 'addTime',hidden : true},
		    {field : 'updateTime',hidden : true},
		    {field : 'vistiTimes',hidden : true},
		    {field : 'saleCounts',hidden : true},
		    {field : 'minBuyNum',hidden : true},
		    {field : 'packageNum',hidden : true},
		    {field : 'packageInfo',hidden : true},
		    {field : 'specification',hidden : true},
		    {field : 'imageUrl1',hidden : true},
		    {field : 'imageUrl2',hidden : true},
		    {field : 'imageUrl3',hidden : true},
		    {field : 'imageUrl4',hidden : true},
		    {field : 'imageUrl5',hidden : true},
			{field : 'productName',	title : '商品',	width : 80,align : 'center',
				formatter: function(value,row,index){
					console.log(IMAGE_BASE_URL+row.imageUrl1);
					return 	'<div style="width:100%;min-width:80px"><div style="width:20%;height:100%;float:left;margin-left:10%;"><img src="'+IMAGE_BASE_URL+row.imageUrl1+'"  width="60px" height="60px" style="margin-right: 0px;float:right"/></div>'+'<div style="width:50%;height:100%;float:left;margin-left: 0px;padding-left:0px; text-align:left"><ul style="float:left"><li><span>'+value+'</span></li><li>商家编码：<span>'+row.productCode+'</span></li></ul></div></div>';
				}
			},
			{field : 'description',	title : '商品描述',		width : 100,align : 'center',
				formatter: function(value,row,index){
					return 	value;
				}
			},
			{field : 'saleStatus',title : '商品状态',	width : 40,align : 'center',hidden : false,
				formatter: function(value,row,index){
					if(value == 0){
						return '回收站';
					}else if(value == 1){
						return '出售中';
					}else if(value == 2){
						return '下架区';
					}else if(value == 3){
						return '仓库中';
					}else{
						return '未知';
					}
				},
			},
			{field : 'cz',title : '操作',	width : 50,align : 'center',hidden : false,
				formatter: function(value,row,index){
				    return '<a href="#" style="color:blue;margin-left:3px;" onClick="deleteItemForever('+row.id+')">彻底删除</a>';
				}
			}
		] ],
		toolbar : [ {
			text : '还原到出售区',
			iconCls : 'icon-edit',
			handler : function() {
				returnItem(1);
			}
		},{
			text : '还原到下架区',
			iconCls : 'icon-edit',
			handler : function() {
				returnItem(2);
			}
		},{
			text : '还原到仓库里',
			iconCls : 'icon-edit',
			handler : function() {
				returnItem(3);
			}
		},  {
			text : '彻底删除',
			iconCls : 'icon-cancel',
			handler : function() {
				deleteItemForever();
			}		
		}, ]
	});
}

/**
 * 查询商品列表
 * @param haveparams 是否有参数，初试加载传入false，点击查询按钮传入true
 */
function selectItemRecycleBin(haveparams){
	var params = {};
	var saleStatus = [0];
	if(haveparams){
		var productName=$.trim($("#recyclebin_productName").val());          		//商品名称
		var categoryId=$.trim($('#recyclebin_categoryId').combobox('getValue'));                  //商品分类
		var brandId=$.trim($('#recyclebin_brandId').combobox('getValue'));                 		//品牌
		var productCode=$.trim($("#recyclebin_productCode").val());                //商家编码
		var addTime_begin=$.trim($('#recyclebin_addTime_begin').datetimebox('getValue'));            //添加时间 开始
		var addTime_end=$.trim($('#recyclebin_addTime_end').datetimebox('getValue'));            	//添加时间 结束
		params = {
			saleStatus : saleStatus,
			productName : productName,
			categoryId : categoryId,
			brandId : brandId,
			productCode : productCode,
			addTime_begin : addTime_begin,
			addTime_end : addTime_end
		};
	}else{
		params = {
				saleStatus : saleStatus
			};
	}
	$("#itemrecyclebin_dategrid").datagrid_pagination($("#itemrecyclebin_dategrid"),path+"/item/v1/list",params,"","");
}

/**
 * 查询所有品牌（用于下拉菜单）
 */
function selectItemBrandsRecycleBin(){
	$.ajax({
		type:"post",
		contentType:"application/json; charset=utf-8",
		url:path+'/item/v1/selectitembrand?page=1&rows=10000',
		data:'{}',
		dataType:"json",
		success:function(data){
			$("#recyclebin_brandId").combobox({
				valueField : 'id',
				textField : 'brandName',
				data : data.rows
			});
		}
   });
}

/**
 * 查询所有类别（用于下拉树）
 */
function selectItemCatTreeRecycleBin(){
	$.ajax({
		type:"post",
		contentType:"application/json; charset=utf-8",
		url:path+'/item/v1/cat/list',
		data:'{}',
		dataType:"json",
		success:function(data){
			$('#recyclebin_categoryId').combotree('loadData', data);
		}
   });
}

/**
 * 得到所选择行的id（int型）
 * @returns {Array}
 */
function getSelectionsIdsRecycleBin(){
	var itemList = $("#itemrecyclebin_dategrid");
	var sels = itemList.datagrid("getSelections");
	var ids = new Array();
	for(var i in sels){
		ids.push(parseInt(sels[i].id));
	}
	return ids;
}

/**
 * 还原商品
 */
function returnItem(saleStatus){
	var ids = getSelectionsIdsRecycleBin();
	if(ids.length == 0){
		$.messager.alert('提示','未选中商品!');
		return ;
	}
	if(ids.length > 1){
		$.messager.alert('提示','只能选择一个商品进行还原!');
		return ;
	}
	row = $("#itemrecyclebin_dategrid").datagrid("getSelected");
	var id = row.id;
	$.messager.confirm('确认','确定还原ID为 '+id+' 的商品吗？',function(r){
	    if (r){
	    	var params = {id:id,saleStatus:saleStatus};
	    	$.ajax({
	    		type:"post",
	    		url:path+'/item/v1/updateiteminfo',
	    		data:params,
	    		dataType:"json",
	    		success:function(data){
	    			if(data.result == 1){
	    				$.messager.alert('提示','还原商品成功!',undefined,function(){
	    					selectItemRecycleBin(false);
	    				});
	    			}else{
	    				$.messager.alert('提示',data.msg);
	    			}
	    		}
	       });
	    }
	});
}

/**
 * 删除商品
 */
function deleteItemForever(id){
	if(id == '' || id == undefined){
		var ids = getSelectionsIdsRecycleBin();
		if(ids.length == 0){
			$.messager.alert('提示','未选中商品!');
			return ;
		}
		id = getSelectionsIdsRecycleBin();
	}else{
		id=[id];
	}
	$.messager.confirm('确认','确定删除ID为 '+id+' 的商品吗？',function(r){
	    if (r){
	    	var params = {ids:id};
	    	$.ajax({
	    		type:"post",
	    		contentType:"application/json; charset=utf-8",
	    		url:path+'/item/v1/deleteiteminfos',
	    		data:JSON.stringify(params),
	    		dataType:"json",
	    		success:function(data){
	    			if(data.result == 1){
	    				$.messager.alert('提示','删除商品成功!',undefined,function(){
	    					selectItemRecycleBin(false);
	    				});
	    			}
	    		}
	       });
	    }
	});
}

